<?php
include("configuration.php");

$smarty -> template_dir = LOCAL_PATH.'components/test/templates/';
$smarty -> compile_dir = LOCAL_PATH.'components/test/templates_c/';

if(!(isset($_SESSION['category_id'])) || $_SESSION['category_id'] == ''){
	$smarty -> assign('component', 'test');
	$smarty -> assign('title','Error');
	$smarty -> assign('warning',"You must select subject to start test.");
	$smarty -> display('error.tpl');
	die("Sorry");
}

$true_values = 0;
$false_values = 0;

$res = mysql_query("select * from user_temp_result where u_id = '".$_SESSION['user_id']."'");
$row = mysql_fetch_array($res);

$questions = array();
$c_colors = array();
$borders = array();
$c_checks = array();

$i = 1;
while($i <= 25){
	$res2 = mysql_query("select * from choices where q_id = '".$row["q".$i."_id"]."' and c_order = '".$row["a".$i."_order"]."'");
	$row2 = mysql_fetch_array($res2);

	$c_temp = array();
	$c_c_temp = array();
	$b_temp = array();
	if($row2['c_correctvalue'] == 1){
	    $c_c_temp[$row2['c_order']-1] = '#';
		$c_temp[$row2['c_order']-1] = '#00FF00';
		$b_temp[$row2['c_order']-1] = '1';
		$true_values++;
	}else {
	    $c_c_temp[$row2['c_order']-1] = 'X';
		$b_temp[$row2['c_order']-1] = '1';
		$c_temp[$row2['c_order']-1] = '#FF0000';
		$false_values++;
	}

	$c = array();
	$res3 = mysql_query("select * from choices where q_id = '".$row["q".$i."_id"]."' order by c_order");
	while($row3 = mysql_fetch_array($res3)){
		if($row3['c_correctvalue'] == 1){
			$c_temp[$row3['c_order']-1] = '#00FF00';
			$c_c_temp[$row3['c_order']-1] = '#';
		}
		$c[] = new Choice($row3["c_title"],0,$row3["c_id"], $row3["c_order"]);
	}
	$c_colors[] = $c_temp;
	$c_checks[] = $c_c_temp;
	$borders[] = $b_temp;
	$res4 = mysql_query("select q_title from questions where q_id = '".$row["q".$i."_id"]."'");
	$row4 = mysql_fetch_array($res4);
	$questions[] = array($i, $row4["q_title"], $c);
	$i++;
}

mysql_query("insert into results (u_id, cat_id, correct_answers, test_taken_date) values('".$_SESSION['user_id']."', '".$_SESSION['category_id']."', '$true_values', now())");
$_SESSION['category_id'] = "";
mysql_query("delete from user_temp_result where u_id ='".$_SESSION['user_id']."'");

$smarty -> assign('component', 'test');
$smarty -> assign('title','Check');
$smarty -> assign(true_values," You have <font color='#00FF00'>".$true_values."</font> correct");
$smarty -> assign(false_values," and <font color='#FF0000'>".$false_values."</font> wrong answers");
$smarty -> assign(c_colors,$c_colors);
$smarty -> assign(c_checks, $c_checks);
$smarty -> assign(borders,$borders);
$smarty -> assign(questions, $questions);

$smarty -> display('check.tpl');
?>
